package com.demo.personalaccountingbackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.personalaccountingbackend.data.R;
import com.demo.personalaccountingbackend.entity.User;
import com.demo.personalaccountingbackend.service.UserService;
import com.demo.personalaccountingbackend.vo.UserVO;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 类描述信息
 *
 * @author zgx
 * @version 1.0
 * @date 2025/10/23 15:27
 */
@Slf4j
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 根据ID查询用户
     */
    @GetMapping("/{id}")
    public R<User> getById(@PathVariable Long id) {
        User user = userService.getById(id);
        return R.ok(user);
    }

    /**
     * 根据用户名查询用户
     */
    @GetMapping("/username/{username}")
    public R<User> getByUsername(@PathVariable String username) {
        User user = userService.findByUsername(username);
        return R.ok(user);
    }

    /**
     * 分页查询用户列表
     */
    @GetMapping("/page")
    public R<Page<User>> page(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<User> page = new Page<>(pageNum, pageSize);
        return R.ok(userService.page(page));
    }

    /**
     * 新增用户
     */
    @PostMapping
    public R<Boolean> save(@RequestBody @Valid User user) {
        boolean result = userService.save(user);
        return R.ok(result);
    }

    /**
     * 修改用户
     */
    @PutMapping
    public R<Boolean> update(@RequestBody @Valid User user) {
        boolean result = userService.updateById(user);
        return R.ok(result);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public R<Boolean> delete(@PathVariable Long id) {
        boolean result = userService.removeById(id);
        return R.ok(result);
    }

    /**
     * 获取当前登录用户信息
     */
    @GetMapping("/info")
    public R<UserVO> getCurrentUserInfo() {
        UserVO userVO = userService.getCurrentUserInfo();
        return R.ok(userVO);
    }

    /**
     * 获取用户个人信息（个人资料）
     */
    @GetMapping("/profile")
    public R<UserVO> getUserProfile() {
        UserVO userVO = userService.getCurrentUserInfo();
        return R.ok(userVO);
    }
}
